import * as resourceService from '../../services/resource/resource';
import { queryIpParamList } from '../../services/resource/ipParam';
import { queryAllList } from '../../services/resource/resourceGroup';
import {qryById} from '../../services/resource/resourceGroup';
import { create } from 'domain';

export default {
    namespace: 'resource',
    state: {
        list: [],
        current: 1,
        currentSize: 10,
        total: 0,
        modalVisible: false,
        modalType: 'create',
        newKey: '',
        currentItem: {},
        viewModalVisible: false,
        ipParamIdList: [],
        resourceGroupIdList: [],
        viewItem:{},
        viewList:[],
    },
    reducers: {
        queryListSuc(state, { payload }) {
            return { ...state, ...payload };
        },
        queryIpParamListSuc(state, { payload: ipParamIdList }) {
            return { ...state, ipParamIdList };
        },
        queryResourceGroupListSuc(state, { payload: resourceGroupIdList }) {
            return { ...state, resourceGroupIdList };
        },
        showModal(state, { payload }) {
            return { ...state, ...payload, newKey: new Date().getTime() + '', modalVisible: true }
        },
        hideModal(state) {
            return { ...state, modalVisible: false, currentItem:{}, newKey: new Date().getTime() + '' }
        },
        showViewModal(state, { payload }) {
            return { ...state, ...payload, viewModalVisible: true }
        },
        hideViewModal(state) {
            return { ...state, viewModalVisible: false, }
        },
    },
    effects: {
        *queryList({ payload }, { call, put }) {
            const { body } = yield call(resourceService.queryList, payload);
            yield put({ type: 'queryListSuc', payload: { list: body.data, current: body.current, currentSize: body.size, total: body.iTotalRecords } });
        },

        *queryIpParamList({ payload }, { call, put }) {
            const { body } = yield call(queryIpParamList);
            yield put({ type: 'queryIpParamListSuc', payload: body.data });
        },
        *queryResourceGroupList({ payload }, { call, put }) {
            const { body } = yield call(queryAllList);
            yield put({ type: 'queryResourceGroupListSuc', payload: body.data });
        },
        *qryById({ payload }, { call, put }) {
            const { body } = yield call(resourceService.qryById, payload);
            yield put({ type: 'showModal', payload: { currentItem: body.data, modalType: 'edit', } });
        },

        *onViewItem({ payload }, { call, put }) {
            const { body } = yield call(qryById, payload);
            yield put({
              type: 'showViewModal', payload: { viewItem: body.data, viewList:body.data.resourceGroupAssets },
            });
          },

        *create({ payload }, { call, put }) {
            const { body } = yield call(resourceService.create, payload.data);
            yield put({ type: 'reload'});
            yield put({ type: 'hideModal' });
        },
        *edit({ payload }, { call, put }) {
            const { body } = yield call(resourceService.edit, payload.data);
            yield put({ type: 'queryList', payload: payload.search });
            yield put({ type: 'hideModal' });
        },
        *remove({ payload }, { call, put }) {
            const { body } = yield call(resourceService.remove, payload.id);
            yield put({ type: 'queryList', payload: payload.search });
        },
        *reload(action, { put }) {
            yield put({ type: 'queryList', payload: { 'name': '' } });
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname }) => {
                if (pathname === '/resource') {
                    dispatch({ type: 'queryList', payload: { 'name': '' } });
                    // dispatch({ type: 'queryIpParamList' });
                    dispatch({ type: 'queryResourceGroupList', payload: {} });
                }
            });
        },
    },
};
